package com.jt.sp04.order.service;

import com.jt.sp01.entity.Item;
import com.jt.sp01.entity.Order;
import com.jt.sp01.entity.User;
import com.jt.sp01.service.OrderService;
import com.jt.sp01.web.util.JsonResult;
import com.jt.sp04.order.feign.ItemClient;
import com.jt.sp04.order.feign.UserClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;


    @Override
    public Order getOrder(String orderId) {
        log.info("获取订单,orderId" +orderId);
        // 调用user-service获取用户信息
        /* 流程
        * 浏览器--》OrderController --》OrderServiceImpl--》itemClient--》
        * ItemController--》 ItemServiceImpl
        */
        JsonResult<List< Item >> items=itemClient.getItems(orderId);

        // 调用item-service获取商品信息
        JsonResult<User> user=userClient.getUser(8);
        //真实项目中，要获取数据。


        Order order = new Order();
        order.setId(orderId);
        order.setUser(user.getData());
        order.setItems(items.getData());
        return order;
    }

    @Override
    public void addOrder(Order order) {
        // 调用item-service减少商品库存
        itemClient.decreaseNumber(order.getItems());


        // 调用user-service增加用户积分
        userClient.addScore(order.getUser().getId(), 1000);

        log.info("保存订单："+order);
    }
}